package com.intozhou.yfl.engine;

import com.infrastructure.activity.BaseActivity;
import com.infrastructure.net.DefaultThreadPool;
import com.infrastructure.net.HttpRequest;
import com.infrastructure.net.RequestCallback;
import com.infrastructure.net.RequestParameter;
import com.infrastructure.net.URLData;
import com.infrastructure.net.UrlConfigManager;

import java.util.List;

/**
 * Created by zyt on 16/6/14 14:24 .
 * Email : syy19930112@aliyun.com
 * Description:
 * <p/>
 * 网络请求入口类.其他成员无需关系底层实现,只需要调用{@link this#invoke(BaseActivity, String, List, RequestCallback)}方法即可执行网络请求.
 */
public class RemoteService {

    private static RemoteService service = null;

    private RemoteService() {

    }

    public static synchronized RemoteService getInstance() {
        if (RemoteService.service == null) {
            RemoteService.service = new RemoteService();
        }
        return RemoteService.service;
    }

    /**
     * 执行网络请求,不需要回调
     *
     * @param activity
     * @param apiKey
     * @param params
     */
    public void invoke(final BaseActivity activity,
                       final String apiKey,
                       final List<RequestParameter> params) {
        invoke(activity, apiKey, params, null);
    }

    /**
     * 执行网络请求,需要回调
     *
     * @param activity
     * @param apiKey   MobileAPI接口关键字
     * @param params   请求参数集合
     * @param callBack 请求结果回调.如果为null,则认为不需要回调.
     */
    public void invoke(final BaseActivity activity,
                       final String apiKey,
                       final List<RequestParameter> params,
                       final RequestCallback callBack) {
        final URLData urlData = UrlConfigManager.findURL(activity, apiKey);
        HttpRequest request = activity.getRequestManager().createRequest(
                urlData, params, callBack);
        DefaultThreadPool.getInstance().execute(request);
    }
}
